from .meta import DocMeta
from datetime import datetime
from mongoengine import Document, EmbeddedDocument, StringField, EmbeddedDocumentField, ListField, signals


class Goals(EmbeddedDocument):
    name = StringField(required=True)
    formula = StringField(Required=True)


class Groups(EmbeddedDocument):
    condition = StringField(required=True)
    result = StringField(required=True)
    line = ListField(required=True)


class Experiments(Document):
    name = StringField(required=True, max_length=200)
    goals = ListField(EmbeddedDocumentField(Goals), default=[])
    groups = ListField(EmbeddedDocumentField(Groups), default=[])
    doc_meta = EmbeddedDocumentField(DocMeta, default=DocMeta())

    @classmethod
    def pre_save(self, sender, document, **kwargs):
        if kwargs["is_new"]:
            document.doc_meta.create_date = datetime.now()
        document.doc_meta.update_date = datetime.now()


signals.pre_save.connect(Experiments.pre_save, sender=Experiments)
